package demo09StackQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:咭咭熊
 * Date:2021 - 10 - 18
 * Time:19:36
 */

import java.util.Arrays;
import java.util.Stack;

/***
 * 1.栈的常用方法：
 *  (1)push()
 * （2）pop()
 *  (3)peek
 *  (4)empty()
 *  (5)search()
 */
public class MyStack {
    private int[] elem;
    private int top;//既可以代表下标，又可以表示当前栈的大小

    public MyStack(){
        this.elem = new int[10];
    }
    public boolean isFull(){
        return  this.top == this.elem.length;
    }

    public int push(int data){
        //判断栈是否满了
        if(isFull()){
            return -1;
        }
        this.elem[top] = data;
        this.top++;
        return this.elem[this.top - 1];
    }

    public int pop(){
        int ret = this.elem[top];
        this.top--;
        return ret;
    }

    public int peek() {
        return this.elem[top - 1];
    }

    public boolean empty(){
      return this.top == 0;
    }

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(0);
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.pop());//0
        System.out.println(myStack.peek());//1
        System.out.println(myStack.empty());//false

    }
}
